package com.game.spacetrader.TradeGoods;

import com.game.spacetrader.Game;
import com.game.spacetrader.R;
import com.game.spacetrader.map.MapView;

import java.util.Random;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TabHost;
import android.widget.Toast;

/**
 * Contains tabs for Market and Hull
 */
@SuppressWarnings("deprecation")
public class TradeGoodsView extends TabActivity {
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tradegoods);
		
		Random random = new Random();
		int randomInt = random.nextInt(5);
		switch(randomInt) {
		case 1: this.encounterDialog("You ran into the Police!!! You lose 10 points");
				Game.getInstance().getPlayer().setPoints(Game.getInstance().getPlayer().getPoints() - 10);
				break;
		case 2: this.encounterDialog("You ran into Pirates!!! You lose 10 points");
				Game.getInstance().getPlayer().setPoints(Game.getInstance().getPlayer().getPoints() - 10);
				break;
		}
		
		TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
		
		tabHost.addTab(tabHost.newTabSpec("tab1")
		        .setIndicator("Hull")
		        .setContent(new Intent(this, TradeGoodsHull.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
		 tabHost.addTab(tabHost.newTabSpec("tab2")
		        .setIndicator("Market")
		        .setContent(new Intent(this, TradeGoodsMarket.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
	}
	
	/**
	 * Creates option menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_space_trader, menu);
	    return true;
	}
	
	/**
     * Listener for the options menu
     * @param item the item in the Menu that is clicked
     */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.travel:
	        	Intent i = new Intent(this, MapView.class);
	        	startActivity(i);
	            return true;
	        case R.id.market:
	        	Intent i2 = new Intent(this, TradeGoodsView.class); startActivity(i2);
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    
    /**
	 * Called when pirates or police are ran into
	 * 
	 * @param string
	 */
	public void encounterDialog(String string) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(TradeGoodsView.this);
		alertDialogBuilder.setTitle("Alert!!!");
			
		alertDialogBuilder
			.setMessage(string)
			.setCancelable(false)
			.setPositiveButton("Okay",new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int id) {
					dialog.cancel();
				}
		  });
	      AlertDialog alertDialog = alertDialogBuilder.create();
		  alertDialog.show();
	}
}
